﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UnityScriptEngine;

namespace MBB_Payroll_Terminal
{
    public partial class frmImportData : Form
    {
        UnityProcessor proc = new UnityProcessor();
        Threader t = new Threader();
        UnityScript empProf = new UnityScript("GETPROF", 5);
        public frmImportData()
        {
            InitializeComponent();
            InitializeFrameSliderForm();
        }
        private void InitializeForm()
        {
            proc.ProcessCompleted += new ProcessHandler(proc_ProcessCompleted);
            proc.ProcessError += new ProcessHandler(proc_ProcessError);

            btnImport.Click += new EventHandler(btnImport_Click);
            btnSave.Click += new EventHandler(btnSave_Click);
            btnLoad.Click += new EventHandler(btnLoad_Click);
        }

        void btnLoad_Click(object sender, EventArgs e)
        {
            if (System.IO.File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\employee.elt"))
            {
                States(false);
                empProf.Script_Clear();
                empProf.Script_Add(Script.PROC_NAME, "GETPROF");
                empProf.Script_Add(Script.LFFPU_READ, Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\employee.elt", "elayorbulc");
                proc.Run(empProf);
            }
        }

        void btnSave_Click(object sender, EventArgs e)
        {
            States(false);
            empProf.Script_Clear();
            empProf.Script_Add(Script.PROC_NAME, "SAVE");
            empProf.Script_Add(Script.LFFPU_WRITE, Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\employee.elt", "elayorbulc");
            proc.Run(empProf);
        }

        void btnImport_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Employee Profile|*.elt|All Files|*.*";
            dlg.CheckFileExists = true;
            dlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            dlg.Title = "Load Employee Profile";
            dlg.RestoreDirectory = true;
            if (dlg.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                States(false);
                empProf.Script_Clear();
                empProf.Script_Add(Script.PROC_NAME, "GETPROF");
                empProf.Script_Add(Script.LFFPU_READ, dlg.FileName, "elayorbulc");
                proc.Run(empProf);
            }
            
        }

        void proc_ProcessError(string id, string desc)
        {
            t.SafeCall(this, delegate
            {
                MessageBox.Show(desc, "Import Employee File", MessageBoxButtons.OK, MessageBoxIcon.Error);
            });
        }

        void proc_ProcessCompleted(string id, string desc)
        {
            t.SafeCall(this, delegate
            {
                switch (id)
                {
                    case "GETPROF":
                        proc.Data_Fetch(out empProf);
                        grdLst.DataSource = empProf.ToDataTable();
                        string[] cols = new string[] { "Emp.No", "Emp.Name", "Department", "Status" };
                        for (int i = 0; i < cols.Length; i++)
                        {
                            grdLst.Columns[i].HeaderText = cols[i];
                        }
                        States(true);
                        break;
                    case "SAVE":
                        States(true);
                        break;

                }
            });
        }
        void States(bool state)
        {
            grpEmployeeFile.Enabled = state;
            grdLst.Enabled = state;
        }
    }
}
